package com.iotproject.studyservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.iotproject.commonutils.JwtUtils;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.entity.UserRememberWord;
import com.iotproject.studyservice.entity.UserStudyWord;
import com.iotproject.studyservice.entity.excel.StudyWordData;
import com.iotproject.studyservice.listener.StudyWordExcelListener;
import com.iotproject.studyservice.mapper.StudyWordMapper;
import com.iotproject.studyservice.service.StudyGradeService;
import com.iotproject.studyservice.service.StudyWordService;
import com.iotproject.studyservice.service.UserRememberWordService;
import com.iotproject.studyservice.service.UserStudyWordService;
import com.iotproject.studyservice.utils.DataUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 单词表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-06
 */
@Service
public class StudyWordServiceImpl extends ServiceImpl<StudyWordMapper, StudyWord> implements StudyWordService {

    @Autowired
    UserStudyWordService userStudyWordService;

    @Autowired
    UserRememberWordService userRememberWordService;

    //获取用户id
    @Override
    public String getMemberUserId(HttpServletRequest httpServletRequest) {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        return memberIdByJwtToken;
    }

    //根据等级得到相关比例
    @Override
    public Map<String, Double> progress(String grade, HttpServletRequest httpServletRequest) {
        QueryWrapper<StudyWord> studyWordQueryWrapper = new QueryWrapper<>();
        studyWordQueryWrapper.eq("grade", grade);
        int wordNumber = baseMapper.selectCount(studyWordQueryWrapper);
        //获取用户id
        String memberUserId = getMemberUserId(httpServletRequest);
        QueryWrapper<UserStudyWord> userStudyWordMapperQueryWrapper = new QueryWrapper<>();
        userStudyWordMapperQueryWrapper.eq("user_id", memberUserId);
        userStudyWordMapperQueryWrapper.eq("grade", grade);
        int studyNumber = userStudyWordService.count(userStudyWordMapperQueryWrapper);
        QueryWrapper<UserRememberWord> userRememberWordQueryWrapper = new QueryWrapper<>();
        userRememberWordQueryWrapper.eq("user_id", memberUserId);
        userRememberWordQueryWrapper.eq("grade", grade);
        int rememberNumber = userRememberWordService.count(userRememberWordQueryWrapper);
        int forgetNumber = wordNumber - rememberNumber;

        Map<String, Double> percent = new HashMap<>();
        percent.put("studyPercent", DataUtils.percent(studyNumber, wordNumber));
        percent.put("rememberPercent", DataUtils.percent(rememberNumber, wordNumber));
        percent.put("forgetPercent", DataUtils.percent(forgetNumber, wordNumber));

        return percent;
    }

    @Override
    public void importStudyWordData(MultipartFile file, StudyWordService studyWordService, StudyGradeService studyGradeService) {
        try {
            //读取Excel 表数据
            InputStream fileStream = file.getInputStream();
            EasyExcel.read(fileStream, StudyWordData.class, new StudyWordExcelListener(studyWordService, studyGradeService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
